# -*- coding: utf-8 -*-
"""
使用示例 - 演示如何使用LLM对话系统

@author: AI Assistant
"""
import sys
from pathlib import Path
from loguru import logger

# 添加项目路径
project_root = Path(__file__).resolve().parent
sys.path.insert(0, str(project_root))

from services import ConversationManager


def main():
    """主函数 - 演示对话流程"""
    
    logger.info("=" * 60)
    logger.info("LLM 对话系统使用示例")
    logger.info("=" * 60)
    
    # 初始化对话管理器
    manager = ConversationManager()
    
    # 示例：使用一个已存在的session_id（需要从数据库中获取）
    # 你需要替换为实际的session_id
    session_id = "your-session-id-here"
    
    logger.info(f"\n开始对话 - Session ID: {session_id}")
    
    # 第一轮对话
    user_message_1 = "请将初始水位设置为100米"
    logger.info(f"\n用户: {user_message_1}")
    
    response_1 = manager.continue_conversation(
        session_id=session_id,
        user_message=user_message_1
    )
    
    if response_1 and response_1.get("success"):
        logger.info(f"助手: {response_1['message']}")
        logger.info(f"对话ID: {response_1['conversation_id']}")
        
        if response_1.get("parameter_changes"):
            logger.info(f"参数修改: {response_1['parameter_changes']}")
        
        # 第二轮对话（使用同一个conversation_id）
        conversation_id = response_1['conversation_id']
        user_message_2 = "再把蒸发系数调整为0.8"
        logger.info(f"\n用户: {user_message_2}")
        
        response_2 = manager.continue_conversation(
            session_id=session_id,
            user_message=user_message_2,
            conversation_id=conversation_id
        )
        
        if response_2 and response_2.get("success"):
            logger.info(f"助手: {response_2['message']}")
            
            if response_2.get("parameter_changes"):
                logger.info(f"参数修改: {response_2['parameter_changes']}")
            
            # 查看对话历史
            logger.info("\n查看对话历史...")
            history = manager.get_conversation_history(conversation_id)
            
            if history:
                logger.info(f"共有 {len(history)} 条消息")
                logger.info(f"对话记录保存在: storage/conversations/{conversation_id}.csv")
    else:
        logger.error(f"对话失败: {response_1.get('message') if response_1 else '未知错误'}")
    
    logger.info("\n" + "=" * 60)
    logger.info("示例结束")
    logger.info("=" * 60)


if __name__ == "__main__":
    main()
